/**
 * 
 */
package com.qq.spirit;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.qq.spirit.inter.AbstractActionAfterHttpRequest;
import com.qq.spirit.inter.TaskUnit;
import com.qq.spirit.model.UserData;
import com.qq.spirit.model.XmlDataUtil;
import com.qq.spirit.model.XmlNodeData;

/**
 * 宴会任务
 * 
 * @author beedoorwei
 * 
 */
public class DinnerTask extends TaskUnit
{

	public DinnerTask()
	{
		this.username = "宴会任务";
	}

	@Override
	public void doTask()
	{
		try
		{
			Iterator<String> ite = UserCache.userQueue.keySet().iterator();
			while (ite.hasNext())
			{
				String username = ite.next();
				TaskManager.addTask(new Dinner(username));
			}
		} catch (Exception e)
		{
			LoggerUtil.error(e);
		}
	}

	/**
	 * 宴会任务
	 * 
	 * @author Administrator
	 * 
	 */
	class Dinner extends TaskUnit
	{
		public Dinner(String username)
		{
			this.username = username;
		}

		public void doTask()
		{
			try
			{
				if (isInDinnerTime())
				{
					UserData u = UserCache.get(username);
					if (u.getUserState() == UserState.LOGIN_GAME)
					{
						HttpUtil.doGet(username, GameConstants.GET_DINNER_URL, new PreDinnerAction(username));
					} else
					{
						// 添加登录任务先
						TaskManager.addTask(new LoginTask(username, this));
					}
				}
			} catch (Exception e)
			{
				LoggerUtil.error(e);
			}
		}
	}

	/**
	 * 是否是宴会时间
	 * 
	 * @return
	 */
	private boolean isInDinnerTime()
	{
		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		if (hour >= 10 && hour < 14)
		{
			return true;
		}
		if (hour >= 19 && hour < 21)
		{
			return true;
		}
		return false;
	}

	/**
	 * 宴会前查询
	 * 
	 * @author Administrator
	 * 
	 */
	class PreDinnerAction extends AbstractActionAfterHttpRequest
	{
		public PreDinnerAction(String username)
		{
			super();
			this.username = username;
			this.taskUnit = new Dinner(username);
		}

		@Override
		public void doCall(List<NameValuePair> headerList, XmlNodeData dinnerInfo)
		{
			UserData u = UserCache.get(username);
			u.setDinnerInfo(dinnerInfo);
			// 判断是否还有宴会次数
			if (isHaveDinnerTimes(dinnerInfo) && isInDinnerTime())
			{
				doDinner(u, dinnerInfo);
			}
		}

		/**
		 * 开始宴会
		 */
		private void doDinner(UserData u, XmlNodeData dinnerInfo)
		{
			String usernation = GameUtil.getNation(XmlDataUtil.getNodeData(u.getAllPlayInfo(), "results.player.nation"));
			List<XmlNodeData> tempNode = XmlDataUtil.getNodeInfoList(dinnerInfo, "results.team.nation");
			List<XmlNodeData> nationTeamList = new ArrayList<XmlNodeData>();
			if (!tempNode.isEmpty())
			{
				// 不为空，有组队，查查是否有本国籍的队伍
				for (XmlNodeData cn : tempNode)
				{
					if (cn.getNodeValue().indexOf(usernation) > 0)
					{
						// 判断是否已经满了
						String num = XmlDataUtil.getNodeData(cn.getParent(), "num");
						if (GameUtil.convertInt(num) < 7)
						{
							nationTeamList.add(cn.getParent());
						}
					}
				}
			}

			if (nationTeamList.isEmpty())
			{
				// 创建组队，并且在5秒后查看队伍是否已经满了
				List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
				nvpList.add(new NameValuePair("dinnerType", "0"));
				HttpUtil.doPost(username, GameConstants.DO_DINNER_TEAM_URL, nvpList, new DinnerTeamAction(username));
			} else
			{
				String teamid = XmlDataUtil.getNodeData(nationTeamList.get(0), "teamid");
				// 加入组队，并且在10秒后查看，判断是否有
				List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
				nvpList.add(new NameValuePair("teamid", teamid));
				HttpUtil.doPost(username, GameConstants.DO_DINNER_TEAM_URL, nvpList, new DinnerTeamAction(username));
			}
		}

		/**
		 * 判断是否还有宴会次数
		 * 
		 * @param dinnerInfo
		 * @return
		 */
		private boolean isHaveDinnerTimes(XmlNodeData dinnerInfo)
		{
			String times = XmlDataUtil.getNodeData(dinnerInfo, "results.normaldinner.num");
			if (GameUtil.convertInt(times) > 0)
			{
				return true;
			}
			return false;
		}
	}

	class DinnerTeamAction extends AbstractActionAfterHttpRequest
	{
		public DinnerTeamAction(String username)
		{
			super();
			this.username = username;
			this.taskUnit = new Dinner(username);
		}

		@Override
		public void doCall(List<NameValuePair> headerList, XmlNodeData dinnerInfo)
		{
			String createDinnerTeamResult = GameUtil.getResultState(dinnerInfo);
			if (createDinnerTeamResult.equals("1"))
			{
				// 创建组队成功
				LoggerUtil.info(username + "创建宴会组队成功");
				// 提交5秒钟后刷新任务
				TaskManager.addTask(new Dinner(username), 10);
			} else
			{
				String message = XmlDataUtil.getNodeData(dinnerInfo, "results.message");
				// 创建组队失败
				LoggerUtil.info(username + "创建宴会组队失败，原因:" + message);

			}
		}
	}

}
